package com.wdd.common.exception;

import com.wdd.common.Result.Result;
import com.wdd.common.Result.ResultUtil;
import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;

/**
 * 异常
 * @author globalMr
 */
@ControllerAdvice
public class TotalExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(TotalExceptionHandler.class);

    public static final Integer ERROR_CODE = 500;   // 发生异常的code

    /**
     * shiro 授权异常
     * @param e
     * @return
     */
    @ExceptionHandler(AuthorizationException.class)
    @ResponseBody
    public Result authorizationException(HttpServletRequest request, AuthorizationException e){
        logger.info("处理 authorizationException");
        // 定义返回内容
        String errMsg = "";
        //获取错误中中括号的内容
        String message = e.getMessage();
        String msg=message.substring(message.indexOf("[")+1,message.indexOf("]"));
        //判断是角色错误还是权限错误
        if (message.contains("role")) {
            errMsg = "对不起，您没有" + msg + "角色";
        } else if (message.contains("permission")) {
            errMsg = "对不起，您没有" + msg + "权限";
        } else {
            errMsg =  "对不起，您的权限有误";
        }
        logger.error(request.getRequestURI() + " | " + errMsg);
        return ResultUtil.error(ERROR_CODE, request.getRequestURI(), errMsg);
    }

    /**
     * 参数校验异常
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    public Result constraintViolationException(HttpServletRequest request ,ConstraintViolationException e){
        logger.info("处理 constraintViolationException");
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        StringBuilder msgBuilder = new StringBuilder();
        msgBuilder.append("发生参数校验异常！原因是:");

        for (ConstraintViolation constraintViolation : constraintViolations) {
            msgBuilder.append(constraintViolation.getMessage()).append(",");
        }
        String errMsg = msgBuilder.toString();
        if (errMsg.length() > 1) {
            errMsg = errMsg.substring(0, errMsg.length() - 1);
        }
        logger.error(request.getRequestURI() + " | " + errMsg);
        return ResultUtil.error(ERROR_CODE, request.getRequestURI(), errMsg);
    }

    /**
     * 空指针异常
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public Result nullPointerException(HttpServletRequest request, NullPointerException e){
        logger.info("处理 nullPointerException");
        String errMsg = "发生空指针异常，原因是 ： " + e;
        logger.error(errMsg);
        return ResultUtil.error(ERROR_CODE, request.getRequestURI(), errMsg);
    }

    /**
     * 总异常
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result exception(HttpServletRequest request, Exception e){
        logger.info("处理 exception");
        String errMsg = "发生异常，原因是 ： " + e;
        logger.error(errMsg);
        return ResultUtil.error(ERROR_CODE, request.getRequestURI(), errMsg);
    }



}
